package com.chuzhong.sf.demo;

import com.chuzhong.sf.gui.MyButton;
import com.chuzhong.sf.gui.MyButtonEditor;
import com.chuzhong.sf.gui.MyButtonRender;
import com.chuzhong.sf.gui.MyEvent;

import javax.swing.*;

import java.awt.event.ActionEvent;
/**
 * @Author 初衷.
 * @Date 2020/10/18
 **/
public class TestTable {


    public  static  void main ( String [ ] args )  {
        JFrame frame = new JFrame("test");
        JTable table = new JTable(5, 4);
        //实例化MyEvent，这里的invoke写你自己处理按钮被点击的逻辑
        MyEvent e = new MyEvent() {
            @Override
            public void invoke(ActionEvent e) {
                MyButton button = (MyButton)e.getSource();
                //打印被点击的行和列
                System.out.println("row:" + button.getRow() + "column :" + button.getColumn());
            }

        };
        //设置表格的渲染器
        table.getColumnModel().getColumn(2).setCellRenderer(new MyButtonRender());

        MyButtonEditor editor = new MyButtonEditor(e);
        //设置表格的编辑器
        table.getColumnModel().getColumn(2).setCellEditor(editor);


        frame.getContentPane().add(table);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }



}
